﻿package net.wangit.dev.unit;

import net.wangit.context.ActionContext;
import net.wangit.dev.DomainAction;
import net.wangit.dev.ObjectChecker;
import net.wangit.jdbc.DB;
import net.wangit.util.Checker;

public class DeleteUnitAction extends DomainAction {

	private static final String SQL_CHECK = "select l from UNIT where uid=?";
	private static final String SQL_UNIT = "delete from UNIT where uid=?";
	private static final String SQL_ELEMENT = "delete from ELEMENT where uid=?";

	public DeleteUnitAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		String unitId = ac.getRequestParameter("uid");
		if (!Checker.isEmpty(unitId)) {
			net.wangit.MapList list = db.query(DeleteUnitAction.SQL_CHECK,
					unitId, 2);
			if (ObjectChecker.invalid(ac, list, "l"))
				return;
			db.execute(DeleteUnitAction.SQL_ELEMENT, unitId, 2);
			db.execute(DeleteUnitAction.SQL_UNIT, unitId, 2);
			UnitCacheUtil.clearAll(domain, unitId);
			UnitReferenceUtil.update(db, domain, unitId, "");
		}
	}
}
